﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.wMall.Buyer
{
    public partial class wfApplyDjack : msBsc.Cls.BPCustom
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControls();
            }
        }
        private void BindControls()
        {
            string djackId = msBsc.SvRef.UserInfo.Operator;
            using (msBsc.AppLogic.DataAlc.CBizCustomerRegister customerRegister = new AppLogic.DataAlc.CBizCustomerRegister())
            {
                DataSet ds = customerRegister.GetCustomerById(djackId);
                if (ds == null || ds.Tables[0].Rows.Count < 1)
                {
                    return;
                }
                int activated = 0;
                activated = Convert.ToInt32(ds.Tables[0].Rows[0]["ACCOUNT_IS_ACTIVATED"]);
                //int.TryParse(ds.Tables[0].Rows[0]["ACCOUNT_IS_ACTIVATED"].ToString(), out activated);
                if (activated == 0)
                {
                    string account = ds.Tables[0].Rows[0]["CUSTOMER_REGISTER_ACCOUNT"].ToString();
                    string pwd = ds.Tables[0].Rows[0]["CUSTOMER_REGISTER_PWD"].ToString();
                    pwd = msBsc.AppLogic.Cfg.DesCry.uncMe(pwd, "zbhpass");
                    string invitation = ds.Tables[0].Rows[0]["BE_INVITATION_CODE"].ToString();
                    string activation = ds.Tables[0].Rows[0]["ACCOUNT_ACTIVATION_CODE"].ToString();
                    string strurl = msBsc.AppLogic.Cfg.DesCry.encMe(account + "," + pwd + "," + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "," + invitation + "," + activation, "rmdes");
                    string url = msBsc.AppLogic.Cfg.SystemFileDir.FileDir.httpUrl + "wp_AccountVerify.htm?KEY=" + strurl;

                    string content = @"<div style='width:600px;font-size:14px; font-family:微软雅黑;padding-left:5px;text-decoration:none;'><div><span style='color:#1a9bf2'>{0}</span>,您好:</div><div style='padding-left:30px;'><div>感谢您使用地接客旅游网!</div><div>请点击如下连接，以完成您帐号的激活：</div><a style='text-decoration:none; color:#1a9bf2;' href='{1}'>{2}</a><div>(如果不能点击该连接地址，请复制并粘贴到浏览器的地址输入框)</div><div style='margin-left:400px;'>地接客旅游网</div><div style='margin-left:400px;'>{3}</div></div></div>";
                    msBsc.SvRef.SendEMail sendEmail = new msBsc.SvRef.SendEMail();
                    sendEmail.SendEmailTo(account, account, "地接客旅游帐户验证", string.Format(content, account, url, url, DateTime.Now.ToShortDateString()));
                    //this.Alert("您的账户没有激活，我们已将激活信息发送到您的邮箱，请去邮箱激活。");
                    this.ExecJScript(@"alert('您的账户没有激活，我们已将激活信息发送到您的邮箱，请去邮箱激活');window.location.href = "" / """);
                }
            }
        }
        private string country = string.Empty;//游客所属国家
        private string countryName = string.Empty;
        private string city = string.Empty;//游客所属省份
        private string cityName = string.Empty;
        private string destion = string.Empty;//游客所属目的地
        private string destionName = string.Empty;

        private void getUserInfo()
        {
            string userId = msBsc.SvRef.UserInfo.Operator;
            using (msBsc.AppLogic.DataAlc.CBizCustomerBaseInfo userInfo = new AppLogic.DataAlc.CBizCustomerBaseInfo())
            {
                DataSet ds = userInfo.GetVisitorByIds(userId);
                if (ds != null)
                {
                    string ret = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 50, "");
                    this.ExecJScript(@"showUserInfo(" + ret + ")");
                    country = ds.Tables[0].Rows[0]["CUSTOMER_COUNTRY_ID"].ToString();
                    countryName = ds.Tables[0].Rows[0]["COUNTRYNAME"].ToString();
                    city = ds.Tables[0].Rows[0]["CUSTOMER_CITY_ID"].ToString();
                    cityName = ds.Tables[0].Rows[0]["CITYNAME"].ToString();
                    destion = ds.Tables[0].Rows[0]["CUSTOMER_DISTINATION_ID"].ToString();
                    destionName = ds.Tables[0].Rows[0]["DISTINANAME"].ToString();
                }
            }
        }

        #region 初始化地区下拉列表

        private void showPlace1()
        {
            string parentsId = this.GetParamHtValue("parentsId");

            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(parentsId);
                int listNum = ds.Tables[0].Rows.Count;
                StringBuilder[] cityArray = packageCityArray(ds);
                StringBuilder listValue = new StringBuilder();
                StringBuilder listOption = new StringBuilder();
                listValue = cityArray[0];
                listOption = cityArray[1];
                if (string.IsNullOrEmpty(country))
                {
                    country = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                    countryName = ds.Tables[0].Rows[0]["COUNTRY_CITY_NME"].ToString();
                }
                this.ExecJScript(@"setArea('country',200,'1'," + listNum.ToString() + "," + listValue.ToString() + "," + listOption.ToString() + ",'" + country + "','" + countryName + "');");
                showPlace2(country);
            }

        }
        private StringBuilder[] packageCityArray(DataSet ds)
        {
            StringBuilder[] cityArray = new StringBuilder[2];
            StringBuilder listValue = new StringBuilder();
            StringBuilder listOption = new StringBuilder();
            listValue.Append(@"[");
            listOption.Append(@"[");
            bool isFirst = true;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (isFirst)
                {
                    isFirst = false;
                }
                else
                {
                    listValue.Append(",");
                    listOption.Append(",");
                }
                listValue.Append(@"""" + ds.Tables[0].Rows[i]["COUNTRY_CITY_ID"] + @"""");
                listOption.Append(@"""" + ds.Tables[0].Rows[i]["COUNTRY_CITY_NME"] + @"""");
            }
            listValue.Append(@"]");
            listOption.Append(@"]");
            cityArray[0] = listValue;
            cityArray[1] = listOption;
            return cityArray;
        }
        private void showPlace2(string strPlace1Id)
        {
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(strPlace1Id);
                int listNum = ds.Tables[0].Rows.Count;
                StringBuilder[] cityArray = packageCityArray(ds);
                StringBuilder listValue = new StringBuilder();
                StringBuilder listOption = new StringBuilder();
                listValue = cityArray[0];
                listOption = cityArray[1];
                if (string.IsNullOrEmpty(city))
                {
                    city = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                    cityName = ds.Tables[0].Rows[0]["COUNTRY_CITY_NME"].ToString();
                }
                this.ExecJScript(@"setArea('city',200,'2'," + listNum.ToString() + "," + listValue.ToString() + "," + listOption.ToString() + ",'" + city + "','" + cityName + "');");
                showPlace3(city);
            }
        }
        private void showPlace3(string strPlace2Id)
        {
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(strPlace2Id);
                string isAll = this.GetParamHtValue("isAll");
                if (isAll.ToLower() == "true")
                {
                    DataRow dr = ds.Tables[0].NewRow();
                    dr["COUNTRY_CITY_ID"] = "";
                    dr["COUNTRY_CITY_NME"] = "全部";
                    ds.Tables[0].Rows.InsertAt(dr, 0);
                }
                int listNum = ds.Tables[0].Rows.Count;
                StringBuilder[] cityArray = packageCityArray(ds);
                StringBuilder listValue = new StringBuilder();
                StringBuilder listOption = new StringBuilder();
                listValue = cityArray[0];
                listOption = cityArray[1];
                if (string.IsNullOrEmpty(destion))
                {
                    destion = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                    destionName = ds.Tables[0].Rows[0]["COUNTRY_CITY_NME"].ToString();
                }
                this.ExecJScript(@"setArea('destion',200,'3'," + listNum.ToString() + "," + listValue.ToString() + "," + listOption.ToString() + ",'" + destion + "','" + destionName + "');");
            }
        }
        #endregion

        #region 地区下拉列表改变
        private void getArea2()
        {
            string parentsId = this.GetParamHtValue("parentsId");

            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(parentsId);
                string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 10000, "");
                this.ExecJScript(@"updateSelet('city'," + retJson + ");");
                getArea3(ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString());
            }
        }
        private void getArea3(string getArea3Id)
        {
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                string isAll = this.GetParamHtValue("isAll");
                DataSet ds = cbcc.SelectCountryByParentId(getArea3Id);
                if (isAll.ToLower() == "true")
                {
                    DataRow dr = ds.Tables[0].NewRow();
                    dr["COUNTRY_CITY_ID"] = "";
                    dr["COUNTRY_CITY_NME"] = "全部";
                    ds.Tables[0].Rows.InsertAt(dr, 0);
                }
                string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 10000, "");
                this.ExecJScript(@"updateSelet('destion'," + retJson + ");");
            }
        }
        #endregion

        private void applyDjack()
        {
            string HeadPhoto = this.GetParamHtValue("HeadPhoto");
            string NickName = this.GetParamHtValue("NickName");
            string ChineseName = this.GetParamHtValue("ChineseName");
            //string 
            string Gender = this.GetParamHtValue("Gender");
            string ServiceType = this.GetParamHtValue("ServiceType");
            string ServiceLabel = this.GetParamHtValue("ServiceLabel");
            string SelfIntro = this.GetParamHtValue("SelfIntro");
            string MobilePhone = this.GetParamHtValue("MobilePhone");
            string Phone = this.GetParamHtValue("Phone");
            string TencentQQ = this.GetParamHtValue("TencentQQ");
            string Email = this.GetParamHtValue("Email");
            string WeChat = this.GetParamHtValue("WeChat");
            string cardType = this.GetParamHtValue("cardType");
            string cardNumber = this.GetParamHtValue("cardNumber");
            string destionId = this.GetParamHtValue("destionId");
            string cityId = this.GetParamHtValue("cityId");
            string countryId = this.GetParamHtValue("countryId");
            string userId = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(NickName) || string.IsNullOrEmpty(MobilePhone) || string.IsNullOrEmpty(Email) || string.IsNullOrEmpty(destionId) || string.IsNullOrEmpty(cityId) || string.IsNullOrEmpty(countryId) || string.IsNullOrEmpty(userId))
            {
                this.Alert("想要成为地接客先必须完善加*号的信息哦!");
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizDjackSpecieInfo specieInfo = new AppLogic.DataAlc.CBizDjackSpecieInfo())
            {
                int intServiceType = 1;
                int.TryParse(ServiceType, out intServiceType);
                msBsc.AppLogic.DataAlc.cls.CommonEnum.CredentialsTypeEnum cardTypeE = (msBsc.AppLogic.DataAlc.cls.CommonEnum.CredentialsTypeEnum)Convert.ToInt32(cardType);
                string[] retArray = specieInfo.BecomeDjack(userId, intServiceType, SelfIntro, Phone, MobilePhone, ChineseName, Gender, cardNumber, HeadPhoto, NickName, destionId, cityId, countryId, ServiceLabel, TencentQQ, Email, WeChat, cardTypeE);
                if (retArray[0] != "TRUE")
                {
                    this.Alert(retArray[1]);
                }
                else
                {
                    msBsc.SvRef.UserInfo.doSetAuthCookie(msBsc.SvRef.UserInfo.Operator, "2", "", NickName);
                    this.ExecJScript(@"window.location.href=""/ws_DjackInfo.htm""");
                }
            }
        }

        public override void DoAction()
        {
            if (this.strAction == "getUserInfo")
            {
                getUserInfo();
                showPlace1();
            }
            else if (this.strAction == "getArea2")
            {
                getArea2();
            }
            else if (this.strAction == "getArea3")
            {
                string parentsId = this.GetParamHtValue("parentsId");
                getArea3(parentsId);
            }
            else if (this.strAction == "applyDjack")
            {
                applyDjack();
            }
        }
    }
}